package my.cn.bdqn.life;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import my.cn.bdqn.life.data.DataManager;
import my.cn.bdqn.life.net.URLParam;
import my.cn.bdqn.life.net.URLProtocol;
import my.cn.bdqn.life.net.impl.URLProtocolImpl;

import org.apache.http.client.methods.HttpPost;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class DramaPlayActivity extends Activity {
	
	private JSONObject json;
    public static List<DataManager.Play> data=new ArrayList();
	private ListView playList;
	private BaseAdapter adapter;
	private Runnable getPhoto=new Runnable(){
		public void run(){
			getData();
		}
	};
	private Handler handler=new Handler(){
		@Override
		public void handleMessage(Message msg){
			switch(msg.what){
			case 0:
				adapter.notifyDataSetChanged();
				break;
			}
		}
	};
	protected TextView addr;
    @Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.paly_layout);
		playList=(ListView)findViewById(R.id.playlist);
		getAdapter();
		playList.setAdapter(adapter);
		new Thread(getPhoto).start();
		handler.sendEmptyMessage(0);
		playList.setOnItemClickListener(new ListView.OnItemClickListener(){

			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				DataManager.Play f=data.get(pos);
				Intent intent=new Intent(DramaPlayActivity.this,DetialPlay.class);
				intent.putExtra("data",f);
				intent.putExtra("pos", pos);
				intent.putExtra("flag", "drama");
				startActivity(intent);
			}});
	}
    private void getAdapter(){
    	adapter=new BaseAdapter(){

			public int getCount() {
				// TODO Auto-generated method stub
				return data.size();
			}

			public Object getItem(int pos) {
				// TODO Auto-generated method stub
				return data.get(pos);
			}

			public long getItemId(int pos) {
				// TODO Auto-generated method stub
				return pos;
			}

			public View getView(int pos, View view, ViewGroup arg2) {
				// TODO Auto-generated method stub
				view=getLayoutInflater().inflate(R.layout.play_item_layout,null);
				ImageView photo,tag;
				TextView name,addr,time;
				photo=(ImageView)view.findViewById(R.id.play_photo);
				tag=(ImageView)view.findViewById(R.id.play_tag);
				name=(TextView)view.findViewById(R.id.play_name);
				addr=(TextView)view.findViewById(R.id.play_addr);
				time=(TextView)view.findViewById(R.id.play_time);
				DataManager.Play m=data.get(pos);
				name.setText(m.getName());
				addr.setText(m.getAddr());
				time.setText(m.getTime());
				Bitmap bm=BitmapFactory.decodeFile("/mnt/sdcard/freelife/"+m.getImage());
				photo.setImageBitmap(bm);
				if(pos<4){
					tag.setImageResource(R.drawable.hot);
				}
				return view;
			}    		
    	};    	
    }
	private void getData(){
		Map<String, String> map = new HashMap();
		map.put("cmd","701");		
		HttpPost post = URLParam.createURL(map);
		URLProtocol urlP = new URLProtocolImpl();
	    JSONObject json = urlP.getData(map);
		int code=1;
		try{
		 code=json.getInt("code");
		 if(code==0){
			 JSONArray array=json.getJSONArray("list");
			 for(int i=0;i<array.length();i++){
				 JSONObject o=array.getJSONObject(i);
				 DataManager.Play play=new DataManager.Play(o.getInt("pid")
						 , o.getInt("image")
						 , o.getString("name")
						 , o.getString("time")
						 , o.getString("addr"));
				 int image=play.getImage();
					File file=new File("/mnt/sdcard/freelife/"+image);
					if(!file.exists()){
						URL url=new URL("http://118.186.211.92:8080/LeisureLifeServer/dealcmd?cmd=4&image="+image);
						InputStream is=url.openStream();
						if(!file.getParentFile().exists()){
							file.getParentFile().mkdirs();
						}
						FileOutputStream out=new FileOutputStream(file);
						byte[]buf=new byte[1024];
						int len;
						while((len=is.read(buf))!=-1){
							out.write(buf,0,len);
						}
						out.close();
						is.close();					
					}
				 data.add(play);
			 }
		 }	
		}catch(Exception e){}
				
	}


}
